package dao;

import java.util.List;

import org.hibernate.Query;

import util.ValidateUtil;
import model.User;

public class UserDao extends BaseDao<User> {
	
	public void delete(Integer id){
		String hql = "DELETE FROM User u WHERE u.id = ?";
		getSession().createQuery(hql).setInteger(0, id).executeUpdate();
	}
	
	public List<User> getAll(){
		String hql = "FROM User u LEFT OUTER JOIN FETCH u.position";		
		return getSession().createQuery(hql).list();
	}
	
	public void saveOrUpdate(User user){
		getSession().saveOrUpdate(user);
	}
	
	public User getUserByEmail(String email){
		String hql = "FROM User u WHERE u.email = ?";
		Query query = getSession().createQuery(hql).setString(0, email);
		User user = (User) query.uniqueResult();
		//System.out.println(user.getPosition().getClass().getName());
		return user;
	}
	
	public User get(Integer id){
		return (User) getSession().get(User.class, id);
	}
	
	
	public boolean isRegisted(String email) {
		String hql = "from User u where u.email = ?";
		List<User> list =  this.findEntityByHQL(hql, email);
		return ValidateUtil.isValid(list);
	}
	
	public User getById(Integer id){
		String hql = "FROM User u where u.id = ?";
		Query query = getSession().createQuery(hql).setInteger(0, id);
		User user = (User) query.uniqueResult();
		return user;
	}
}
